/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.zeuscloudserver.util.encrypt;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;


/**
 *
 * @author
 */
public class DigestUtil
{
    
    /*
	* compute mac
	*/
	public static String mac(String algName, byte[] plain, byte[] key)
	{
		try
		{ 
			SecretKeySpec sk = new SecretKeySpec(key, algName);
			Mac mac = Mac.getInstance(algName);
			mac.init(sk);
			return ByteUtil.toBcdString(mac.doFinal(plain));
		}
		catch (Exception ex)
		{
		}
		return null;
	}
        
        /*
	* HMAC-SHA1
	*/
	public static String hmacSha1(String plain, String key, String charset) throws UnsupportedEncodingException
	{
		return mac( "HmacSha1", plain.getBytes(charset), key.getBytes(charset));
	}
	
	public static String hmacSha1(String plain, String key) throws UnsupportedEncodingException
	{
		return hmacSha1(plain, key, Charsets.UTF8.getName() );
	}
	
	/*
	* HMAC-MD5
	*/
	public static String hmacMd5(String plain, String key, String charset) throws UnsupportedEncodingException
	{
		return mac( "HmacMD5", plain.getBytes(charset), key.getBytes(charset));
	}
	public static String hmacMd5(String plain, String key) throws UnsupportedEncodingException
	{
		return hmacMd5( plain, key, Charsets.UTF8.getName());
	}
}